package com.hzhk.happyrent.ui.holder.card;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.Constants;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.card.CardHotModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.activity.rent.LoadWebViewActivity;
import com.hzhk.happyrent.ui.adapter.card.CardHotAdapter;
import com.hzhk.happyrent.ui.view.MyListView;
import com.hzhk.happyrent.utils.ListUtils;
import com.tamic.novate.Throwable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by soft_zzc on 2017/11/24.
 */

public class CardHotViewHolder {
    private Logger log = LoggerFactory.getLogger(this.getClass().getName());
    @BindView(R.id.ll_card_more)
    LinearLayout llCardMore;
    @BindView(R.id.fragment_card_more)
    TextView fragmentCardMore;
    @BindView(R.id.card_type_hot)
    MyListView cardTypeHot;
    CardHotAdapter cardHotAdapter;
    private Context context;
    private int page = 1;
    private List<CardHotModel.DataBean.CardsBean> mList=new ArrayList<>();
    private CardHotModel cardHotModel;
    public CardHotViewHolder(View convertView, Context context) {
        this.context = context;
        log.debug("jump into CardHotViewHolder");
        ButterKnife.bind(this, convertView);
        initView();
        initData();
        log.debug("jump out CardHotViewHolder");
    }
    private void initData() {
        log.debug("jump into initData");
        Map<String,Object> map=new HashMap<>();
        log.debug("page = "  + page);
        map.put("page",page);
        map.put("limit","10");
        HttpManager.getInstance().get(API.CARD_HOT, map, new MyStringResponse(context) {
            @Override
            public void onError(Object tag, Throwable e) {
//                Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onCancel(Object tag, Throwable e) {

            }
            @Override
            public void onNext(Object tag, String response){
                cardHotModel = new Gson().fromJson(response,CardHotModel.class);
                if(page==1){
                    mList.clear();
                }
                if(!ListUtils.isEmpty(cardHotModel.getData().getCards())){
                    mList.addAll(cardHotModel.getData().getCards());
                    if(cardHotModel.getData().getCards().size()<Constants.PAGE_SIZE){
                        if(page==1){
                            llCardMore.setVisibility(View.GONE);
                        }else{
                            doIt();
                        }
                    }
                }else{
                        doIt();
                }
                cardHotAdapter.setList(mList);
                log.debug("cardHotModel" + cardHotModel.getData().getCards().size());
                //mList.addAll(cardHotModel.getData().getCards());
                //cardHotAdapter.setList(mList);
            }
        });
        log.debug("jump out initData");
    }
    private void doIt(){
        page=1;
        llCardMore.setVisibility(View.GONE);
        Toast.makeText(context,"已经加载全部",Toast.LENGTH_SHORT).show();

    }
    public void setAction(){

    }
    public void initView() {
        log.debug("jump into initView");
        fragmentCardMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                page++;
                initData();
            }
        });
        cardHotAdapter=new CardHotAdapter(context);
        cardTypeHot.setAdapter(cardHotAdapter);
        cardTypeHot.setFocusable(false);
        cardTypeHot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(context, LoadWebViewActivity.class);
                intent.putExtra("uil",((CardHotAdapter)adapterView.getAdapter()).getItem(i).getLink());
                intent.putExtra("name",((CardHotAdapter)adapterView.getAdapter()).getItem(i).getBank_title());
                context.startActivity(intent);
            }
        });
        log.debug("jump out initView");
    }
}
